﻿#include "menuvo.h"



MenuVO::MenuVO(QObject *parent): QBaseEntity(parent)
{
    clear();
}

MenuVO::MenuVO(const MenuVO &other)
{
    id = other.id;
    name = other.name;
    path = other.path;
    redirect = other.redirect;
    component = other.component;
    meta = other.meta;
    children = other.children;
}

MenuVO &MenuVO::operator=(const MenuVO &other)
{
    id = other.id;
    name = other.name;
    path = other.path;
    redirect = other.redirect;
    component = other.component;
    meta = other.meta;
    children = other.children;
    return *this;
}

MenuVO::~MenuVO()
{

}

QJsonObject MenuVO::toJsonObject(PropertiesType type,QStringList propertyList) const
{
    QJsonObject jsonObject;
    const QMetaObject *metaObject = this->metaObject();
    for (int i = 0; i < metaObject->propertyCount(); ++i) {
        QMetaProperty metaProperty = metaObject->property(i);
        if (metaProperty.isReadable()) {
            QVariant value = metaProperty.read(this);
            if (value.isValid()) {
                QString propertyName = metaProperty.name();
                // 忽略 objectName 属性
                if (propertyName == "objectName") {
                    continue;
                }
                if (metaProperty.userType() == qMetaTypeId<QList<MenuVO>>()) {
                    // 处理 QList<MyClass*> 类型的属性
                    QList<MenuVO> list = value.value<QList<MenuVO>>();
                    QJsonArray jsonArray;
                    for (MenuVO &item : list) {
                        jsonArray.append(item.toJsonObject());
                    }
                    jsonObject[propertyName] = jsonArray;
                }else if (metaProperty.userType() == qMetaTypeId<MetaVO>()) {
                    // 处理 QList<MyClass*> 类型的属性
                    MetaVO meta = value.value<MetaVO>();
                    jsonObject[propertyName] = meta.toJsonObject();
                }
                else {
                    jsonObject[propertyName] = qvariantToJsonValue(value);
                }
            }
        }
    }
    return jsonObject;
}

void MenuVO::fromJsonObject(const QJsonObject &jsonObject)
{
    const QMetaObject *metaObject = this->metaObject();
    for (int i = 0; i < metaObject->propertyCount(); ++i) {
        QMetaProperty metaProperty = metaObject->property(i);
        if (metaProperty.isWritable()) {
            QString propertyName = metaProperty.name();
            if (jsonObject.contains(propertyName)) {
                QJsonValue jsonValue = jsonObject[propertyName];
                if (jsonValue.isNull()) {
                    continue;
                }
                if (metaProperty.userType() == qMetaTypeId<QList<MenuVO>>()) {
                    QList<MenuVO> list;
                    QJsonArray jsonArray = jsonValue.toArray();
                    for (const QJsonValue &value : jsonArray) {
                        MenuVO child;
                        child.fromJsonObject(value.toObject());
                        list.append(child);
                    }
                    metaProperty.write(this, QVariant::fromValue(list));
                }else if (metaProperty.userType() == qMetaTypeId<MetaVO>()) {
                    MetaVO meta;
                    meta.fromJsonObject(jsonValue.toObject());
                    metaProperty.write(this,QVariant::fromValue(meta));
                } else {
                    QVariant value = qjsonValueToVariant(jsonValue, metaProperty.type());
                    metaProperty.write(this, value);
                }
            }
        }
    }
}

void MenuVO::fromMenu(Menu menu)
{
    MetaVO meta;
    QVariantMap menuProperties = menu.getAllProperties();
    QVariantMap menuVOProperties = getAllProperties();
    QVariantMap metaVOProperties = meta.getAllProperties();

    for (auto menuVOIt = menuVOProperties.begin(); menuVOIt != menuVOProperties.end(); ++menuVOIt) {
        QVariantMap::iterator menuIt = menuProperties.find(menuVOIt.key());
        if(menuIt != menuProperties.end()){
            this->setProperty(menuVOIt.key().toLatin1(),menuIt.value());
        }
    }

    for (auto metaVOIt = metaVOProperties.begin(); metaVOIt != metaVOProperties.end(); ++metaVOIt) {
        QVariantMap::iterator menuIt = menuProperties.find(metaVOIt.key());
        if(menuIt != menuProperties.end()){
            meta.setProperty(metaVOIt.key().toLatin1(),menuIt.value());
        }
    }
    setMeta(meta);
}

uint64_t MenuVO::getId() const
{
    return id;
}

void MenuVO::setId(const uint64_t &value)
{
    id = value;
}

QString MenuVO::getName() const
{
    return name;
}

void MenuVO::setName(const QString &value)
{
    name = value;
}

QString MenuVO::getPath() const
{
    return path;
}

void MenuVO::setPath(const QString &value)
{
    path = value;
}

QString MenuVO::getRedirect() const
{
    return redirect;
}

void MenuVO::setRedirect(const QString &value)
{
    redirect = value;
}

QString MenuVO::getComponent() const
{
    return component;
}

void MenuVO::setComponent(const QString &value)
{
    component = value;
}


MetaVO MenuVO::getMeta() const
{
    return meta;
}

void MenuVO::setMeta(const MetaVO &value)
{
    meta = value;
}

QList<MenuVO> MenuVO::getChildren() const
{
    return children;
}

void MenuVO::setChildren(const QList<MenuVO> &value)
{
    children = value;
}
